import _request from "../common/_request";
import {Message} from "element-ui";
import _router from "../common/_router";
import cache_user from "../cache/cache_user";
import cache_token from "../cache/cache_token";

let api_user = {

    // 角色列表
    role: function (rid, callback) {
        let data = {};
        data.rid = rid;
        _request._get('/user/role', data, function (response) {
            if (response.code !== _request.success) {
                Message.info(response.msg);
                return;
            }

            callback(response.data)
        })
    },

    // 角色列表
    roles: function (callback) {
        let data = {};
        _request._get('/user/roles', data, function (response) {
            if (response.code !== _request.success) {
                Message.info(response.msg);
                return;
            }

            callback(response.data)
        })
    },

    // code验证码
    code: function ( phone, callback) {
        let data = {};
        data.phone = phone
        _request._get('/user/code', data, function (response) {
            if (response.code !== _request.success) {
                Message.info(response.msg);
                return;
            }
            callback(response.data)
        })
    },

    // 注册
    register: function ( data, callback) {

        _request._post('/user/register', data, function (response) {
            if (response.code !== _request.success) {
                Message.info(response.msg);
                return;
            }

            callback(response.data)
        })
    },

    // 登录
    login: function (callback) {
        let data = {};
        _request._get('/user/login', data, function (response) {
            if (response.code !== _request.success) {
                _router.jmp_error(response.msg)
                return;
            }

            cache_user.set(response.data.user)
            cache_token.set(response.data.token)

            callback(response.data)
        })
    },

    // 员工管理 人员列表
    list: function (data, callback) {

        _request._get('/user/list', data, function (response) {
            if (response.code !== _request.success) {
                Message.info(response.msg);
                return;
            }

            callback(response.data)
        })
    },


    //员工管理权限
    state: function ( state,uid,callback) {

        let data = {};
        data.state =state;
        data.uid =uid;
        _request._get('/user/state', data, function (response) {
            if (response.code !== _request.success) {
                Message.info(response.msg);
                return;
            }
            console.log(response, 'state response')
            callback(response.data)
        })
    },

}


export default api_user
